package com.pdf.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;

/**
 * 生成pdf公共类
 * @author ytj
 * @date 2021/11/21
 */
public class PdfUtil {

    // 定义全局的字体静态变量
    private static Font titlefont;
    private static Font headfont;
    private static Font keyfont;
    private static Font textfont;
    private static Font tablefont;
    // 最大宽度
    private static int maxWidth = 520;
    // 静态代码块
    static {
        try {
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(bfChinese, 16, Font.BOLD);
            headfont = new Font(bfChinese, 14, Font.BOLD);
            keyfont = new Font(bfChinese, 12, Font.NORMAL);
            tablefont=new Font(bfChinese,10,Font.BOLD);
            textfont = new Font(bfChinese, 10, Font.NORMAL);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 打印名单
     * */
    public void nameDoublePdf(Document document) throws Exception{
        // 段落
        Paragraph paragraph = new Paragraph("PDF抬头", headfont);
        paragraph.setAlignment(1); //设置文字居中 0靠左   1，居中     2，靠右
        paragraph.setIndentationLeft(12); //设置左缩进
        paragraph.setIndentationRight(12); //设置右缩进
        paragraph.setFirstLineIndent(24); //设置首行缩进
        paragraph.setLeading(20f); //行间距
        paragraph.setSpacingBefore(5f); //设置段落上空白
        paragraph.setSpacingAfter(10f); //设置段落下空白

        // 表格
        PdfPTable table = createTablesPaent(new float[] { 260,260 });
        table.addCell(createCell("第 1 页，共 1 页", keyfont, Element.ALIGN_RIGHT, 4, false));
        table.addCell(createCell("信息", keyfont, Element.ALIGN_LEFT, 2, false));
        PdfPTable table1=createTables(new float[]{35,70,35,90});
        PdfPTable table2=createTables(new float[]{35,70,35,90});
        table1.addCell(createCell("序号", tablefont, Element.ALIGN_CENTER,1));
        table1.addCell(createCell("会员姓名", tablefont, Element.ALIGN_CENTER,1));
        table1.addCell(createCell("性别", tablefont, Element.ALIGN_CENTER,1));
        table1.addCell(createCell("身份证号", tablefont, Element.ALIGN_CENTER,1));
        table1.addCell(createCell("1", textfont, Element.ALIGN_CENTER,1));
        table1.addCell(createCell("测试", textfont, Element.ALIGN_CENTER,1));
        table1.addCell(createCell("女", textfont, Element.ALIGN_CENTER,1));
        table1.addCell(createCell("123456789987654321", textfont, Element.ALIGN_CENTER,1));

        table2.addCell(createCell("序号", tablefont, Element.ALIGN_CENTER,1));
        table2.addCell(createCell("会员姓名", tablefont, Element.ALIGN_CENTER,1));
        table2.addCell(createCell("性别", tablefont, Element.ALIGN_CENTER,1));
        table2.addCell(createCell("身份证号", tablefont, Element.ALIGN_CENTER,1));
        table2.addCell(createCell("1", textfont, Element.ALIGN_CENTER,1));
        table2.addCell(createCell("测试", textfont, Element.ALIGN_CENTER,1));
        table2.addCell(createCell("女", textfont, Element.ALIGN_CENTER,1));
        table2.addCell(createCell("123456789987654321", textfont, Element.ALIGN_CENTER,1));

        PdfPCell cell = new PdfPCell(table1);
        cell.setVerticalAlignment(Element.ALIGN_LEFT);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setUseBorderPadding(true);
        cell.disableBorderSide(15);//去除全部线条
        table.addCell(cell);

        PdfPCell cell1 = new PdfPCell(table2);
        cell1.setVerticalAlignment(Element.ALIGN_RIGHT);
        cell1.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell1.setUseBorderPadding(true);
        cell1.disableBorderSide(15);
        table.addCell(cell1);

        // 添加图片
        Image image = Image.getInstance("https://img-blog.csdn.net/20180801174617455?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzg0ODcxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70");
        image.setAlignment(Image.ALIGN_CENTER);
        image.scalePercent(40); //依照比例缩放、

        document.add(paragraph);
        document.add(table);
        document.add(image);

    }


/**------------------------创建表格单元格的方法start----------------------------*/
    /**
     * 创建单元格(指定字体)
     * @param value
     * @param font
     * @return
     */
    public PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置字体位置
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }
    /**
     * 创建单元格（指定字体、水平..）
     * @param value
     * @param font
     * @param align
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setMinimumHeight(20);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }
    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并）
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setMinimumHeight(20);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并）
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @return
     */
    public PdfPCell createCell(String value,String nextValue, Font font,int align, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setMinimumHeight(50);
        cell.setRight(10);
        Phrase phrase=new Phrase();
        phrase.add(new Chunk(value,font));
        phrase.add(Chunk.NEWLINE);
        phrase.add(Chunk.NEWLINE);
        phrase.add(new Chunk(nextValue,font));
        cell.setPhrase(phrase);
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、单元格跨x行合并）
     * @param align
     * @param colspan
     * @param rowSpan
     * @return
     */
    public PdfPCell createImageCell(Image image, int align, int colspan,int rowSpan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setRowspan(rowSpan);
        cell.setMinimumHeight(80);
        cell.addElement(image);
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、单元格跨x行合并）
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param rowSpan
     * @return
     */
    public PdfPCell createHeightCell(String value, Font font, int align, int colspan,int rowSpan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setRowspan(rowSpan);
        cell.setMinimumHeight(80);//设置最小行高
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、单元格跨x行合并）
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param rowSpan
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, int colspan,int rowSpan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setRowspan(rowSpan);
        cell.setMinimumHeight(20);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }
    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并）
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, int colspan,float height) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setMinimumHeight(height);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }
    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、单元格跨x行合并）
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param rowSpan
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, int colspan,int rowSpan,float height) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setRowspan(rowSpan);
        cell.setMinimumHeight(height);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    public PdfPCell createCell(){
        PdfPCell cell = new PdfPCell();
        cell.setBorder(0);//设置表格线宽
        cell.setPaddingTop(15.0f);//上间距
        cell.setPaddingBottom(8.0f);//下间距
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）
     * @param value
     * @return
     */
    public PdfPCell createCell(String value) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setColspan(7);
        cell.setPhrase(new Phrase(value,textfont ));
        cell.setBorder(0);
        cell.setPaddingTop(1);
        cell.setPaddingBottom(2.5F);
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @return
     */
    public PdfPCell createTitleCell(String value, Font font, int align, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setPadding(3.0f);
        cell.setBorder(0);
        cell.setPaddingTop(1.0f);
        cell.setPaddingBottom(3f);
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param boderFlag
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setPadding(3.0f);
        if (!boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(15.0f);
            cell.setPaddingBottom(8.0f);
        } else if (boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(0.0f);
            cell.setPaddingBottom(15.0f);
        }
        return cell;
    }
    /**
     * 创建单元格（指定字体、水平..、边框宽度：0表示无边框、内边距）
     * @param value
     * @param font
     * @param align
     * @param borderWidth
     * @param paddingSize
     * @param flag
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        cell.setBorderWidthLeft(borderWidth[0]);
        cell.setBorderWidthRight(borderWidth[1]);
        cell.setBorderWidthTop(borderWidth[2]);
        cell.setBorderWidthBottom(borderWidth[3]);
        cell.setPaddingTop(paddingSize[0]);
        cell.setPaddingBottom(paddingSize[1]);
        if (flag) {
            cell.setColspan(2);
        }
        return cell;
    }
/**------------------------创建表格单元格的方法end----------------------------*/


/**--------------------------创建表格的方法start------------------- ---------*/
    /**
     * 创建默认列宽，指定列数、水平(居中、右、左)的表格
     * @param colNumber
     * @param align
     * @return
     */
    public PdfPTable createTable(int colNumber, int align) {
        PdfPTable table = new PdfPTable(colNumber);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(align);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }
    /**
     * 创建指定列宽、列数的表格
     * @param widths
     * @return
     */
    public PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 创建指定列宽、列数的表格
     * @param widths
     * @return
     */
    public PdfPTable createTablesPaent(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 创建指定列宽、列数的表格
     * @param widths
     * @return
     */
    public PdfPTable createTables(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(230);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }
    /**
     * 创建空白的表格
     * @return
     */
    public PdfPTable createBlankTable() {
        PdfPTable table = new PdfPTable(1);
        table.getDefaultCell().setBorder(0);
        table.addCell(createCell("", keyfont));
        table.setSpacingAfter(20.0f);
        table.setSpacingBefore(20.0f);
        return table;
    }
/**--------------------------创建表格的方法end------------------- ---------*/
}
